/*
 * @Author: 260156 260156@gree.com.cn
 * @Date: 2022-07-25 18:41:40
 * @LastEditors: 260156 260156@gree.com.cn
 * @LastEditTime: 2022-08-15 10:05:12
 * @FilePath: \gree-zst-web\src\store\modules\layout.ts
 * @Description:
 *
 * Copyright (c) 2022 by 260156 260156@gree.com.cn, All Rights Reserved.
 */
import { defineStore } from "pinia";
import { useRouter, useRoute } from "vue-router";

interface TabsItem {
  name: string;
  title: string;
  path: string;
}

interface LayoutStore {
  collapse: boolean;
  tabs: TabsItem[];
}

export const useLayoutStore = () => {
  const router = useRouter();
  const route = useRoute();
  return defineStore("layoutStore", {
    state: (): LayoutStore => ({
      collapse: false,
      tabs: [],
    }),
    actions: {
      toggleCollapse() {
        this.collapse = !this.collapse;
      },
      addTab(data: TabsItem) {
        const hasTab =
          this.tabs.filter((item) => item.path === data.path).length > 0;
        hasTab || this.tabs.push(data);
      },
      /**
       * @description:
       * @param {string} 路由路径path
       * @return {*}
       */
      removeTab(path: string) {
        this.tabs = this.tabs.filter((item, i) => item.path !== path);
        if (path === route.path) {
          this.tabs.length > 0
            ? router.push(this.tabs[this.tabs.length - 1])
            : router.push("home");
        }
      },
    },
    getters: {},
  });
};
